package com.tools.cleanmaster.ui.view.oversroll;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

/**
 * Created by apolunor on 15/11/30.
 */
public class OverScrollListView extends ListView {

    private OverScrollListViewTool mTool;

    public OverScrollListView(Context context) {
        this(context, null);
    }

    public OverScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OverScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOverScrollMode(View.OVER_SCROLL_NEVER);
        mTool = new OverScrollListViewTool(this, true);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTool.handleTouchEventDown(event);
                break;
        }
        return super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                mTool.handleTouchEventCancel(event);
                break;

            case MotionEvent.ACTION_UP:
                if (mTool.handleTouchEventUp(event)) {
                    return true;
                }
                break;

            case MotionEvent.ACTION_MOVE:
                if (mTool.handleTouchEventMove(event)) {
                    return true;
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    public void setForbidOverScrollTop(boolean forbidOverScrollTop) {
        mTool.setForbidOverScrollTop(forbidOverScrollTop);
    }

    public void setForbidOverScrollBottom(boolean forbidOverScrollBottom) {
        mTool.setForbidOverScrollBottom(forbidOverScrollBottom);
    }
}
